package com.kyzh.core.pager.gamelist


import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.base.fragment.BaseVmDbFragment
import com.gushenge.core.base.viewmodel.BaseViewModel
import com.gushenge.core.beans.*
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.gushenge.core.requests.GameRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.adapters.*
import com.kyzh.core.adapters.v3.GMGameListBorderAdapter
import com.kyzh.core.adapters.v3.GameListBorderAdapter
import com.kyzh.core.databinding.FragmentSortNewBinding
import com.kyzh.core.databinding.HeaderTopSortBinding
import com.kyzh.core.databinding.ItemFooterSortBinding
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.startGameDetailActivity
import com.kyzh.core.utils.startGameDetailActivity2
import kotlinx.android.synthetic.main.fragment_sort.*
import org.jetbrains.anko.support.v4.dip
import org.jetbrains.anko.support.v4.onRefresh

/**
 * A simple [Fragment] subclass.
 */
class GMFragment :
    BaseVmDbFragment<BaseViewModel, FragmentSortNewBinding>(R.layout.fragment_sort_new) {
    override fun reloadClick() {

    }

    var position = 0

    // 左侧的
    lateinit var leftAdapter: SortLeftAdapter
    var mp = 1
    var type = 1

    //推荐的头
    lateinit var firstHeader: HeaderTopSortBinding

    // 推荐的adapter
    lateinit var chosenAdapter: SortChosenAdapter1


    //
    lateinit var listAdapter: GMGameListBorderAdapter

    //
    lateinit var bind: ItemFooterSortBinding

    lateinit var sortOtherTypeAdapter: GameListBorderAdapter


    override fun initView(savedInstanceState: Bundle?) {
        firstHeader = HeaderTopSortBinding.inflate(layoutInflater)
        chosenAdapter = SortChosenAdapter1()
        listAdapter = GMGameListBorderAdapter(R.layout.item_game_border, null)
        bind = ItemFooterSortBinding.inflate(layoutInflater)
        sortOtherTypeAdapter =
            GameListBorderAdapter(R.layout.item_game_border, null)
        sortOtherTypeAdapter.setIntentType("gm")
        bind.revFoot.adapter = sortOtherTypeAdapter
        bind.revFoot.layoutManager = LinearLayoutManager(requireContext())
        GameRequest.gmSort {
            loadSir.showSuccess()
            type = it[position].id
            // 顶部banner
            it[position].color = requireActivity().resources.getColor(R.color.colorPrimary)
            it[position].bgcolor = requireActivity().resources.getColor(R.color.white)
            leftAdapter = SortLeftAdapter(R.layout.frag_sort_item, it)
            sort_recyclerview.layoutManager =
                LinearLayoutManager(requireActivity(), RecyclerView.VERTICAL, false)
            sort_recyclerview.adapter = leftAdapter
            // 加载数据
            initChosen()

            leftAdapter.setOnItemClickListener { adapter, view, pos ->
                mp = 1
                this.position = pos
                resetColor(it)
                it[pos].color = requireActivity().resources.getColor(R.color.colorPrimary)
                it[pos].bgcolor = requireActivity().resources.getColor(R.color.white)
                leftAdapter.notifyDataSetChanged()
                type = it[pos].id
                if (position == 0) {
                    initChosen()
                } else {
                    initRight()
                }
                return@setOnItemClickListener
            }
        }

        db.swipe.onRefresh {
            mp = 1
            if (position == 0) {
                initChosen()
            } else {
                initRight()
            }
        }
    }

    // 推荐页
    fun initChosen() {
        GameRequest.chosenGM(mp, type) {
            db.swipe.isRefreshing = false
            chosenAdapter.loadMoreModule.loadMoreComplete()
            if (mp > this.max_p) {
                chosenAdapter.loadMoreModule.loadMoreEnd(true)
            }
            db.right.adapter = chosenAdapter

            chosenAdapter.setNewInstance(this.data)
        }
        chosenAdapter.setOnItemClickListener { adapter, view, position ->
            val any = adapter.data[position] as Game
            startGameDetailActivity2(any.gid)
        }

        chosenAdapter.loadMoreModule.setOnLoadMoreListener {
            mp++
            GameRequest.chosenGM(mp) {
                this.data?.let { chosenAdapter.addData(it) }
                chosenAdapter.loadMoreModule.loadMoreComplete()
                if (mp > this.max_p) {
                    chosenAdapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
    }


    //其他页
    fun initRight() {
        GameRequest.chosenGM(mp, type) {
            right.adapter = listAdapter
            db.swipe.isRefreshing = false

            listAdapter.setNewInstance(this.data)
            listAdapter.loadMoreModule.loadMoreComplete()
            if (mp >= this.max_p) {
                listAdapter.loadMoreModule.loadMoreEnd(true)
            }
            listAdapter.removeAllFooterView()
            if (this.tuijian!!.isNotEmpty()) {

                sortOtherTypeAdapter.setNewInstance(this.tuijian)
                listAdapter.addFooterView(bind.root)
            }
        }
        listAdapter.loadMoreModule.setOnLoadMoreListener {
            mp++
            GameRequest.chosenGM(mp, type) {
                this.data?.let { listAdapter.addData(it) }
                listAdapter.removeAllFooterView()
                listAdapter.loadMoreModule.loadMoreComplete()
                if (mp >= this.max_p) {
                    listAdapter.loadMoreModule.loadMoreEnd(true)
                }
                if (this.tuijian!!.isNotEmpty()) {
                    sortOtherTypeAdapter.setNewInstance(this.tuijian)
                    listAdapter.addFooterView(bind.root)
                }
            }
        }
    }

    /*
       * 重置左边栏颜色
       * */
    private fun resetColor(lists: MutableList<Sort>) {
        for (i in lists) {
            i.color = requireActivity().resources.getColor(R.color.bg_f7)
            i.bgcolor = requireActivity().resources.getColor(R.color.bg_f7)
        }
    }

    override fun lazyLoadData() {
    }


}
